package com.kcs.fxmargin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.action.form.InquiryMarginActionForm;
import com.kcs.fxmargin.model.TDealsSum;
import com.kcs.fxmargin.service.DealsSumService;

public class InquiryMarginAction extends FXAction {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	DealsSumService service = new DealsSumService();

	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		try{
			log.debug("InquiryMarginAction : Start");
			
			InquiryMarginActionForm formBean = (InquiryMarginActionForm) form;
			formBean.setDataNotFound(false);
			
			if("init".equals(formBean.getAction())){
				formBean.resetExpect();
				formBean.resetDealsSum();
			}
			else if("search".equals(formBean.getAction())){
				formBean.resetDealsSum();
				search(formBean);
			}
			
			log.debug("InquiryMarginAction : End");	
		}catch(Exception e){
			log.error(e.getMessage());
			return (mapping.findForward("error"));
		}
		
		
		return mapping.findForward("inquiryMargin");
	}

	public void search(InquiryMarginActionForm formBean) throws Exception {
		
		log.debug("InquiryMarginAction : search : Start");
		
		if(formBean.getCifNo() != null && !"".equals(formBean.getCifNo())) 
			formBean.setDealsSum(service.search(formBean.getCifNo(), formBean.getDate(), formBean.getCashFlag()));
		
		if(formBean.getDealsSum() == null ) {
			formBean.setDealsSum(new TDealsSum());
			formBean.resetDealsSum();
			formBean.setDataNotFound(true);
		}
		
			
		log.debug("InquiryMarginAction : search : End");
	}
}
